package com.blockmeta.bbs.immodule.adapter;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.baselibrary.utils.TimeUtil;
import com.blockmeta.bbs.businesslibrary.util.RouterActivityHelper;
import com.blockmeta.bbs.businesslibrary.widget.CircleImageView;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.bean.beaninterface.IFriendsEntry;
import com.blockmeta.bbs.immodule.widget.FriendsLetterItemDecoration;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * @author AirThor
 * @date 2019/3/6.
 */
public class CircleMembersAdapter extends BaseAdapter<IFriendsEntry, BaseViewHolder> {
    private FriendsLetterItemDecoration mDecoration;
    private boolean mEditable;

    public void setEditable(boolean editable) {
        mEditable = editable;
        mDecoration.setEditable(editable);
        notifyDataSetChanged();
    }

    public CircleMembersAdapter(int layoutResId, @Nullable List<IFriendsEntry> data, CompoundButton.OnCheckedChangeListener checkedListener) {
        super(layoutResId, data);
        mCheckedListener = checkedListener;
    }

    @Override
    public void bindToRecyclerView(RecyclerView recyclerView) {
        super.bindToRecyclerView(recyclerView);
        mDecoration = new FriendsLetterItemDecoration(this);
        recyclerView.addItemDecoration(mDecoration);
    }

    @Override
    protected void convert(BaseViewHolder helper, final IFriendsEntry item) {
        CircleImageView avatar = helper.getView(R.id.contacts_avatar);
        LoadPicUtil.loadWithPicasso(avatar, item.getAvatar());

        helper.setText(R.id.contacts_name, item.getName());

        helper.setText(R.id.contacts_grade,item.getGradeTag());

        helper.setText(R.id.contacts_registration_time,"加入 "+TimeUtil.getUserRegisterAge(item.getRegistrationTime()));

        ToggleButton toggleButton = helper.getView(R.id.tg_selector);
        if (!mEditable){
            toggleButton.setChecked(false);
        }else {
            toggleButton.setChecked(item.getCheckedState());
        }
        item.setCheckedState(toggleButton.isChecked());
        toggleButton.setTag(item);
        toggleButton.setOnCheckedChangeListener(mCheckedListener);

        toggleButton.setVisibility(mEditable?View.VISIBLE:View.GONE);

        avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterActivityHelper.openUserCenter(mContext, String.valueOf(item.getUserId()));
            }
        });
    }

    private CompoundButton.OnCheckedChangeListener mCheckedListener;


    /**
     * 滚动到索引位置
     * @param letter 首字母
     */
    public void scrollToTargetLetter(String letter){
       int position = mDecoration.getLetterPosition(letter);
       if (position==-1){return;}
       getRecyclerView().smoothScrollToPosition(position+getHeaderLayoutCount());
    }

    /**
     * 是否以首字母分组
     */
    public void groupByLetter(boolean group){
        mDecoration.setGroupByLetter(group);
    }

}
